package com.gorkr.dp;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author gorkr
 * @date 2022/06/15 09:59
 **/
public class H354RussianDollEnvelopes {
    public int maxEnvelopes(int[][] envelopes) {
        if (envelopes.length == 0) {
            return 0;
        }

        int length = envelopes.length;

        // 排序
        Arrays.sort(envelopes, new Comparator<int[]>(){
            @Override
            public int compare(int[] a, int[] b){
                return a[0] == b[0]? b[1]-a[1]:a[0]-b[0];
            }

        });
        int[] highs = new int[length];
        for (int i = 0; i < length; i++) {
            highs[i] = envelopes[i][1];
        }

        return lengthOfLTS(highs);

    }

    private int lengthOfLTS(int[] highs) {
        //dp 定义 : 当前位置最长子序列长途
        int[] dp = new int[highs.length];

        // base case
        Arrays.fill(dp,1);
        int res = 1;

        // 状态
        for (int i = 0; i < highs.length; i++) {
            for (int j = 0; j < i; j++) {
                if(highs[i]>highs[j]){
                    dp[i] = Math.max(dp[i],dp[j]+1);
                }
            }
            res = Math.max(res, dp[i]);
        }



        return res;
    }

}
    